from fastapi import APIRouter, Request
from utils import create_json_response

router = APIRouter()

@router.post("/app/version")
@router.get("/app/version")
async def check_app_version(request: Request):
    """检查更新接口 - 返回新版本下载地址"""
    
    # 支持原始数据流格式
    try:
        if request.method == "POST":
            body = await request.body()
            if body:
                import json
                data = json.loads(body.decode('utf-8'))
            else:
                data = {}
        else:
            data = dict(request.query_params)
    except:
        data = {}
    
    # 获取当前版本号（可选参数）
    current_version = str(data.get('version', ''))
    
    # 演示数据：返回空字符串表示无更新
    # 如需演示更新，可设置具体的下载地址
    response_data = {
        "url": "https://gitee.com/jacoder/x-apply/releases/download/XP-1.0.1/debug-1.0.0.apk"  # 为空表示无更新，可设置为具体下载地址如 "https://demo.com/app-v2.0.apk"
    }
    
    return create_json_response(response_data)